package com.kl.modular.generate.controller;

import com.kl.common.util.FreemarkerUtil;
import com.kl.modular.generate.model.config.*;
import com.kl.modular.generate.service.IGenerateService;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 代码自动生成 入口
 * 
 * @author zjb
 * @date 2015-3-30
 */
@Controller
@RequestMapping("generate")
public class GenerateMainController {

	private static final Logger logger = LoggerFactory.getLogger(GenerateMainController.class);

	@Autowired
	private IGenerateService generateService;

	/**
	 * 生成
	 *
	 * @return
	 * @throws TemplateException 
	 * @throws IOException 
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request) throws Exception {
		// 初始化总配置信息
		String modualrName = "cake";
		String dataName = "cake_print";
		String [] exchangePv = new String[]{"order_info"};
		String classFilePath = "C://Users//Administrator//Desktop//xrp";
		String authorstr = "kl_zjb";
		String copyRight = "klsoft";
		String packagePath = "com.kl.modular."+modualrName;

		GenerateCodeConfig codeConfig = new GenerateCodeConfig();// 配置信息
        codeConfig.setDataName(dataName);
        codeConfig.setTableName(exchangePv);// 表 集合
        codeConfig.setClassFilePath(classFilePath);// 生成文件路径
        codeConfig.setPackagePath(packagePath);// 包名路径
        codeConfig.setViewFilePath("");// view JSP 路径
        codeConfig.setAuthor(authorstr);
        codeConfig.setCopyRight(copyRight);
        // 加载model配置信息
        ModelConfig modelConfig = new ModelConfig();
        modelConfig.setIsCreate(BaseConfig.CREATE_YES);
        modelConfig.setPackageName("model");
        modelConfig.setPackageUrl(codeConfig.getPackagePath() + "dao.model");
        modelConfig.setFilePath(codeConfig.getClassFilePath() + "//" + modelConfig.getPackageUrl());
        modelConfig.setFtlUrl(FreemarkerUtil.modelFtlPath);
        // 加载mapper配置信息
        MapperConfig mapperConfig = new MapperConfig();
        mapperConfig.setFileSuffixName("Mapper");
        mapperConfig.setIsCreate(BaseConfig.CREATE_YES);
        mapperConfig.setPackageName("mapper");
        mapperConfig.setPackageUrl(codeConfig.getPackagePath() + "dao.mapper");
        mapperConfig.setFilePath(codeConfig.getClassFilePath() + "//" + mapperConfig.getPackageUrl());
        mapperConfig.setFtlUrl(FreemarkerUtil.mapperFtlPath);
        // 加载xml配置信息
        MapperXmlConfig mapperXmlConfig = new MapperXmlConfig();
        mapperXmlConfig.setIsCreate(BaseConfig.CREATE_YES);
        mapperXmlConfig.setPackageName("mapping");
        mapperXmlConfig.setFtlUrl(FreemarkerUtil.mappingFtlPath);
		mapperXmlConfig.setPackageUrl(codeConfig.getPackagePath() + "dao.mapping");
        mapperXmlConfig.setFilePath(codeConfig.getClassFilePath() + "//" + mapperXmlConfig.getPackageUrl());
        mapperXmlConfig.setFileSuffixName("Mapper");
        // 加载service配置信息
        ServiceConfig serviceConfig = new ServiceConfig();
        serviceConfig.setIsCreate(BaseConfig.CREATE_YES);
        serviceConfig.setFilePreFixName("I");
        serviceConfig.setFileSuffixName("Service");
        serviceConfig.setPackageName("service");
        serviceConfig.setPackageUrl(codeConfig.getPackagePath() + ".service.api");
        serviceConfig.setFilePath(codeConfig.getClassFilePath() + "//" + serviceConfig.getPackageUrl());
        serviceConfig.setFtlUrl(FreemarkerUtil.serviceFtlPath);
		// 加载serviceImpl配置信息
		ServiceImplConfig serviceImplConfig = new ServiceImplConfig();
		serviceImplConfig.setIsCreate(BaseConfig.CREATE_YES);
		serviceImplConfig.setFileSuffixName("ServiceImpl");
		serviceImplConfig.setPackageName("impl");
		serviceImplConfig.setPackageUrl(codeConfig.getPackagePath() + ".service.impl");
		serviceImplConfig.setFilePath(codeConfig.getClassFilePath() + "//" + serviceImplConfig.getPackageUrl());
		serviceImplConfig.setFtlUrl(FreemarkerUtil.serviceImplFtlPath);
		// 加载Controller配置文件
		ControllerConfig controllerConfig = new ControllerConfig();
		controllerConfig.setIsCreate(BaseConfig.CREATE_YES);
		controllerConfig.setFileSuffixName("Controller");
		controllerConfig.setPackageName("controller");
		controllerConfig.setPackageUrl(codeConfig.getPackagePath() + ".controller");
		controllerConfig.setFilePath(codeConfig.getClassFilePath() + "//" + controllerConfig.getPackageUrl());
		controllerConfig.setFtlUrl(FreemarkerUtil.controllerFtlPath);
		// 加载BO配置文件
		BoConfig boConfig =  new BoConfig();
		boConfig.setFileSuffixName("Bo");
		boConfig.setIsCreate(BaseConfig.CREATE_YES);
		boConfig.setPackageName("bo");
		boConfig.setPackageUrl(codeConfig.getPackagePath() + "." + boConfig.getPackageName());
		boConfig.setFilePath(codeConfig.getClassFilePath() + "//"
				+ boConfig.getPackageName());
		boConfig.setFtlUrl(FreemarkerUtil.boFtlPath);
		// 加载VIEW配置文件
		ViewConfig viewConfig = new ViewConfig();
		viewConfig.setIsCreate(BaseConfig.CREATE_YES);
		viewConfig.setPackageName("");
		viewConfig.setFilePath(codeConfig.getClassFilePath());
		viewConfig.setFtlAdd(FreemarkerUtil.viewAddFtlPath);
		viewConfig.setFtlEdit(FreemarkerUtil.viewEditFtlPath);
		viewConfig.setFtlList(FreemarkerUtil.viewListFtlPath);
		viewConfig.setAjaxFtlList(FreemarkerUtil.viewAjaxFtlPath);

		codeConfig.setModelConfig(modelConfig);
		codeConfig.setMapperConfig(mapperConfig);
		codeConfig.setMapperXmlConfig(mapperXmlConfig);
		codeConfig.setServiceConfig(serviceConfig);
		codeConfig.setServiceImplConfig(serviceImplConfig);
		codeConfig.setBoConfig(boConfig);
		codeConfig.setControllerConfig(controllerConfig);
		codeConfig.setViewConfig(viewConfig);
		generateService.createCodeForCRUD(codeConfig, request.getSession().getServletContext());
		return "success.jsp";
	}

}
